package com.horay.common.utils;

import java.security.SecureRandom;
import java.util.Objects;
import java.util.Random;
import java.util.stream.IntStream;

/**
 * @author horay
 * @category 数组工具类
 * @email horay_hyc@qq.com
 * @date 2018/1/31 22:21
 */
public class Arrays {

    /**
     * @category 数组索引交换
     * @param x 数组
     * @param a 索引
     * @param b 索引
     * @author horay
     * @date 2018/1/31 22:23
     * @return void
     */
    public static void swap(Object[] x, int a, int b) {
        Object t = x[a];
        x[a] = x[b];
        x[b] = t;
    }

    /**
     * @category 数组索引交换
     * @param x 数组
     * @param a 索引
     * @param b 索引
     * @author horay
     * @date 2018/1/31 22:23
     * @return void
     */
    public static void swap(int[] x, int a, int b) {
        if (Objects.isNull(x)) {
            throw new NullPointerException();
        }
        int t = x[a];
        x[a] = x[b];
        x[b] = t;
    }

    /**
     * @category 随机数组
     * @param n 长度
     * @param s 开始
     * @param e 结束
     * @author horay
     * @date 2018/1/31 22:31
     * @return int
     */
    public static int[] random(int n, int s, int e) {
        if (e < s) {
            throw new IllegalArgumentException();
        }
        SecureRandom random = new SecureRandom();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = (int) (random.nextDouble() * (e - s + 1)) + s;
        }
        return array;
    }

    /**
     * @category 字符串拼接
     * @param array 数组
     * @author horay
     * @date 2018/1/31 23:11
     * @return java.lang.String
     */
    public static String join(int[] array) {
        StringBuilder sb = new StringBuilder();
        IntStream.of(array).forEach(i -> sb.append(", ").append(i));
        return sb.substring(2).toString();
    }

}
